@extends('admin.admin')

@section('content')
        <!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        用户管理
        <small>用户列表</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="/admin"><i class="fa fa-dashboard"></i> 控制面板</a></li>
        <li ><a href="/admin/user">用户管理</a></li>
        <li class="active">用户列表</li>
    </ol>
</section>

<!-- Main content -->
<section class="content">

    <div class="row">
        <div class="col-xs-12">



            <div class="box box-primary ">
                <div class="box-body table-responsive">

                    {!! Form::model(@$user,array('route' => 'admin.user.index','method' => 'get','class'=>'form-search form-inline')) !!}

                        <div class="form-group">
                            {!! Form::select('role_id',$roleArr,$search['role_id'],['class'=>'form-control','placeholder'=>'所有用户'])!!}
                        </div>

                        <div class="form-group">
                            {!! Form::text('user_name',$search['user_name'],['class'=>'form-control','placeholder'=>'用户名'])!!}
                        </div>

                        <div class="form-group">
                            {!! Form::text('phone',$search['phone'],['class'=>'form-control','placeholder'=>'手机号'])!!}
                        </div>

                        <div class="form-group">
                            {!! Form::text('email',$search['email'],['class'=>'form-control','placeholder'=>'邮箱'])!!}
                        </div>

                        <button type="submit" class="btn btn-primary">搜索</button>
                        <button type="button" onclick="window.location.href='/admin/user'" class="btn btn-default">重置</button>
                    {!! Form::close() !!}
                </div>

            </div><!-- end .box box-primary -->

            <div class="box box-primary">
                <div class="box-header">
                    <h3 class="box-title">用户列表</h3>
                    <!-- <div class="box-tools">
                        <a href="{{route('admin.user.create')}}" type="button" class="btn btn-block btn-success btn-xs"><i class="icon  glyphicon glyphicon-plus"></i> 添加</a>
                    </div> -->
                </div>
                <!-- /.box-header -->
                <div class="box-body table-responsive no-padding">
                    <table class="table table-hover">
                        <tr>
                            <th class="text-center" style="width: 60px">选择</th>
                            <th>ID</th>
                            <th>用户名</th>
                            <th>手机</th>
                            <th>邮箱</th>
                            <th>最后登录时间</th>
                            <th>最后登录IP</th>
                            <th>积分</th>
                            <th>是否封禁</th>
                            <th>操作</th>
                            

                        </tr>
                        @foreach ($users as $user)
                        <tr>
                            <td class="text-center">{!! Form::checkbox('selUser', $user->id, false,['id'=>$user->id]) !!}</td>
                            <td>{{$user->id}}</td>
                            <td>{{$user->user_name}}</td>
                            <td>{{$user->phone}}</td>
                            <td>{{$user->email}}</td>
                            <td>{{$user->last_login_time}}</td>
                            <td>{{$user->last_login_ip}}</td>
                            <!-- <td>{{$user->is_disabled}}</td> -->
                           <!--  <td>
                                @if($user->is_disabled==0)
                                正常
                                @else
                                封禁  http://afuyigou/admin/user_point_log/1
                                @endif
                            </td> -->
                            <td>{{$user->point}}</td>
                            <td width="9%" style="text-align: center" class="is_disabled" id="{{$user->id}}" is_disabled="{{$user->is_disabled}}">@if($user->is_disabled==0)
                                    <i  class="glyphicon glyphicon-eye-open" style=" cursor: pointer;color: green;"></i>
                                @else
                                    <i  class="glyphicon glyphicon-eye-close " style=" cursor: pointer;color: red;"></i>
                                @endif
                            </td>
                            <td>
                                <a href="user_point_log/{{$user->id}}" type="button" class="btn  btn-default btn-xs"><i class="icon fa fa-eye"></i> 查看积分</a>
                            </td>

                            
                        </tr>
                        @endforeach

                    </table>
                </div>
                <!-- /.box-body -->


                <div class="box-footer">
                    <div class="pull-right no-margin-pager">
                        {!! $users->appends(['role_id' => $search['role_id'], 'user_name' => $search['user_name']])->render() !!}
                    </div>
                    <div class="btn-group">
                        <button type="button" id="addRole" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-expanded="false">添加至角色</button>
                    </div>
                </div>


            </div>
            <!-- /.box -->

        </div>
    </div>


    <!-- Modal -->
    <div class="modal fade" id="warmModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close close_btn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">信息提示</h4>
                </div>
                <div class="modal-body" id="errorMessage">

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    </div>


    <!-- admin_role -->
    <div class="modal fade" id="role" tabindex="-1" role="dialog" aria-labelledby="">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close close_btn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">请选择添加到的角色</h4>
                </div>
                <div class="modal-body" id="showRecommend">
                    {!! Form::select('role_id',$roleArr,@$_GET['role_id'],['id'=>'role_id','class'=>'form-control','placeholder'=>'所有角色'])!!}
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default close_btn" data-dismiss="modal">关闭</button>
                    <span id="add_user_to_role" class="btn btn-default" data-dismiss="modal">添加</span>
                </div>
            </div>
        </div>
    </div>

</section>
<!-- /.content -->


<script >
    $('.is_disabled').on('click',function(){
        var el = $(this);
        var id = el.attr('id');
        var url = "{{Route('admin.user.toggleDisabled')}}";
        //alert(url);
        $.ajax({
            url:url+"/?id="+id,
            type:'get',
            dataType:'json',
            success:function(result){
                if(result.info){
                    if(result.is_disabled==0){
                        el.find('.glyphicon').removeClass('glyphicon-eye-close');
                        el.find('.glyphicon').addClass('glyphicon-eye-open');
                        el.find('.glyphicon').css("color","green");

                    }else{
                        el.find('.glyphicon').removeClass('glyphicon-eye-open');
                        el.find('.glyphicon').addClass('glyphicon-eye-close');
                        el.find('.glyphicon').css("color","red");

                    }

                }
            }
        });
    });


    var userArr =[];
    $('#addRole').click(function(){
        userArr =[];

        $('input[name="selUser"]:checked').each(function(){
            userArr.push($(this).val());
        });

        if(userArr.length==0){

            $('#warmModal').modal('show');
            $('#errorMessage').html('您还没有选择任何用户');

        }else{

            $('#role').modal('show');

        }//end if()

    });


    $('#add_user_to_role').click(function(e){

        var dataForm = {};
        var role_id = $('#role_id').val();
        wind_status = 'add_role';

        if(role_id==null || role_id=='' || role_id=='all' ){


            $("#role_id").css("color","red");

            $("#role_id option").each(function(){ //遍历全部option
                $(this).css("color","black");
                $("#role_id option[value='all']").css("color","red");
            });

            //$('#role').modal('show');

        }else{
            wind_status = 'close';

            dataForm.userArr = userArr;

            urls = "/admin/add_user_to_role/"+role_id;

            $.ajax({
                type: "GET",
                url: urls,
                data: dataForm,
                dataType: "json",
                headers: {
                    'X-CSRF-Token': "{{csrf_token()}}"
                },
                async:true,
                cache:false,
                success: function (msg) {

                    //console.log(msg);
                    if(msg.status){

                        $('#warmModal').modal('show');
                        $('#errorMessage').html('添加成功');

                    }//end if()

                }
            }); //end ajax
        }//end if()
    });

</script>

<script>
    $('.btn-danger').on('click',function(){
        console.log('11');
    });
</script>
@endsection